//
//  CommonFunc.m
//  BarCode
//
//  Created by Dale on 17/4/5.
//  Copyright © 2017年 MobileArtisan. All rights reserved.
//

#import "CommonFunc.h"
#import <BmobSDK/Bmob.h>
#import "BarCode-Swift.h"

@implementation CommonFunc

//字符串为空检查
+ (BOOL)isEmptyString:(NSString *)sourceStr {
    if ((NSNull *)sourceStr == [NSNull null]) {
        return YES;
    }
    if (sourceStr == nil) {
        return YES;
    }
    if (sourceStr == NULL) {
        return YES;
    }
    if ([sourceStr isEqual:[NSNull null]]) {
        return YES;
    }
    if (![sourceStr isKindOfClass:[NSString class]]) {
        return YES;
    }
    if([sourceStr isEqualToString:@"<null>"]){
        return YES;
    }
    if ([sourceStr isEqualToString:@"null"]) {
        return YES;
    }
    if ([sourceStr isEqualToString:@""]) {
        return YES;
    }
    if (sourceStr.length == 0) {
        return YES;
    }
    return NO;
}


//MARK:- request network
+ (void)getUsersWithResult:(HandleBlock)block {
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"person"];
    bquery.limit = 1000;
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if(error == nil) {
            NSLog(@"get users from network:%@", array);
            [Bridge2OC removeUserList];
            for (BmobObject *result in array) {
                User *tmpUser = [[User alloc] init];
                NSDictionary *dic = @{
                                      @"username":[result objectForKey:@"username"],
                                      @"objectId":result.objectId,
                                      @"full_name":[result objectForKey:@"full_name"],
                                      @"role":[result objectForKey:@"role"],
                                      @"staff_code":[result objectForKey:@"staff_code"]?[result objectForKey:@"staff_code"]:@""
                                      };
                [tmpUser transeferDic2UserWithDic:dic];
                [Bridge2OC appendUserListWithUser:tmpUser];
            }
            block([Bridge2OC getUserList], 1);
        } else {
            block(error.localizedDescription, 0);
        }
    }];
}

+ (void)bind:(NSString *)staffCode FullName:(NSString *)fullName Department:(NSString *)department Code:(NSString *)code GoodsName:(NSString *)goodsName Block:(HandleBlock)block {
    //绑定接口
    BmobObject *user2codeObj = [BmobObject objectWithClassName:@"user2code"];
    [user2codeObj setObject:staffCode forKey:@"user_id"];
    [user2codeObj setObject:fullName forKey:@"full_name"];
    [user2codeObj setObject:department forKey:@"department"];
    [user2codeObj setObject:code forKey:@"code"];
    [user2codeObj setObject:goodsName forKey:@"goods_name"];
    [user2codeObj saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if(error == nil) {
            if (isSuccessful) {
                block(@"绑定成功", 1);
            } else {
                block(@"绑定失败", 0);
            }
        } else {
            block(error.localizedDescription, 0);
        }
    }];
}

//更新库存
+ (void)updateStockCountsWithGoodsId:(NSString *)goodsId Block:(HandleBlock)block {
    //查找goods表
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"goods"];
    [bquery whereKey:@"goods_code" equalTo:goodsId];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        //没有返回错误
        if (!error) {
            //对象存在
            if (array && array.count > 0) {
                BmobObject *object = array.firstObject;
                NSInteger counts = [[object objectForKey:@"counts"] integerValue];
                if (counts >= 1) {
                    [object setObject:[NSNumber numberWithInteger:counts - 1] forKey:@"counts"];
                    //异步更新数据
                    [object updateInBackground];
                    block(@"更新库存成功", 1);
                } else {
                    block(@"库存不足，无法更新", 0);
                }
            } else {
                block(@"没有此商品", 0);
            }
        }
        else{
            //进行错误处理
            block(error.localizedDescription, 0);
        }
 
    }];
}


+ (void)findGoodsName:(NSString *)code Block:(HandleBlock)block {
    //绑定接口
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"goods"];
    [bquery whereKey:@"goods_code" equalTo:code];
    [bquery whereKey:@"counts" greaterThan:[NSNumber numberWithInt:0]]; //age大于0
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error != nil) {
            block(error.localizedDescription, 0);
        } else {
//            block([((BmobObject *)array.firstObject) objectForKey:@"goods_name"], 1);
            block((BmobObject *)array.firstObject, 1);
        }
    }];
}

+ (void)modifyPasswordWithOldPassword:(NSString *)oldPassword newPassword:(NSString *)newPassword block:(HandleBlock)block {
    BmobUser *user = [BmobUser currentUser];
    [user updateCurrentUserPasswordWithOldPassword:oldPassword newPassword:newPassword block:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            block(@"修改成功，请重新登录", 1);
            //用新密码登录
//            [BmobUser loginInbackgroundWithAccount:user.username andPassword:newPassword block:^(BmobUser *user, NSError *error) {
//                if (error) {
//                    block(error.localizedDescription, 0);
//                } else {
//                    block(@"修改成功，请重新登录", 1);
//                }
//            }];
        } else {
            NSLog(@"change password error:%@",error);
            block(error.localizedDescription, 0);
        }
    }];
}
    
    
@end
